/**
 * StateChangeSupport.java
 * @author Daniel Liu
 * Create: 2006-12-22 ����09:07:38
 */
package org.lex.utils;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

public class StateChangeSupport {
	protected EventListenerList listenerList = new EventListenerList();

	public void addChangeListener(ChangeListener l) {
		listenerList.add(ChangeListener.class, l);
	}

	public void removeChangeListener(ChangeListener l) {
		listenerList.remove(ChangeListener.class, l);
	}

	public ChangeListener[] getChangeListeners() {
		return this.listenerList.getListeners(ChangeListener.class);
	}

	// Notify all listeners that have registered interest for
	// notification on this event type.
	public void fireStateChanged(Object source) {
		this.fireStateChanged(new ChangeEvent(source));
	}

	public void fireStateChanged(ChangeEvent e) {
		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first
		// notifying those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == ChangeListener.class) {
				((ChangeListener) listeners[i + 1]).stateChanged(e);
			}
		}
	}
}
